package com.yanqu.road.entity.activity.tonbingchannel.join;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_tonbing_union_join")
public class ChannelTonBingUnionJoin extends DataObject {

    @FieldTypeAnnotation(desc = "活动ID", select = true, update = true)
    private int activityId;
    @FieldTypeAnnotation(desc = "商会UID", update = true)
    private String unionUid;
    @FieldTypeAnnotation(desc = "商会信息")
    private UnionBaseInfo unionBaseInfo;
    @FieldTypeAnnotation(desc = "商会成员")
    private Map<Long, ChannelTonBingUnionJoinMember> memberMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "阵营ID")
    private int campId;
    @FieldTypeAnnotation(desc = "渠道")
    private int channelId;

    // 缓存，门客赚钱
    private long totalAbility = 0L;

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public String getUnionUid() {
        return unionUid;
    }

    public void setUnionUid(String unionUid) {
        if (unionUid == null) {
            return;
        }
        if (this.unionUid == null || !this.unionUid.equals(unionUid)) {
            this.unionUid = unionUid;
            setOp(DataStatus.Update);
        }
    }

    public UnionBaseInfo getUnionBaseInfo() {
        return unionBaseInfo;
    }

    public void setUnionBaseInfo(UnionBaseInfo unionBaseInfo) {
        if (null == unionBaseInfo) {
            return;
        }
        if (this.unionBaseInfo == null || !this.unionBaseInfo.equals(unionBaseInfo)) {
            this.unionBaseInfo = unionBaseInfo;
            setOp(DataStatus.Update);
        }
    }

    public Map<Long, ChannelTonBingUnionJoinMember> getMemberMap() {
        return memberMap;
    }

    public void setMemberMap(Map<Long, ChannelTonBingUnionJoinMember> memberMap) {
        if (null == memberMap) {
            return;
        }
        this.memberMap = memberMap;
        setOp(DataStatus.Update);
    }

    public int getCampId() {
        return campId;
    }

    public void setCampId(int campId) {
        if (this.campId != campId) {
            this.campId = campId;
            setOp(DataStatus.Update);
        }
    }

    // ==============================

    public long getTotalAbility() {
        return totalAbility;
    }

    public void setTotalAbility(long totalAbility) {
        if(totalAbility != this.totalAbility){
            this.totalAbility = totalAbility;
            setUpdateOption();
        }
    }

    public void setChannelId(int channelId) {
        if(channelId != this.channelId){
            this.channelId = channelId;
            setUpdateOption();
        }
    }

    // ==============================
    public int getChannelId(){
        if(channelId != 0){
            return channelId;
        }
        List<Long> userIdList = new ArrayList<>(memberMap.keySet());
        if(userIdList.size() > 0){
            channelId = memberMap.get(userIdList.get(0)).getChannelId();
        }
        return channelId;
    }

    public long getServerId(){
        List<Long> userIdList = new ArrayList<>(memberMap.keySet());
        if(userIdList.size() > 0){
            return memberMap.get(userIdList.get(new Random().nextInt(userIdList.size()))).getServerId();
        }
        return 0;
    }
}
